<!DOCTYPE html>

<?php
session_start();
$frist=!count($_POST);
?>

<html>
    <head>
        <meta charset="UTF-8">
        <title></title>
        <script type="text/javascript" src="js/login.js">
        </script>
        
    </head>
    <body>
        <?php
        if($frist){
            
        
        ?>
        <form action="login.php" method="post" id="form">
            <h1>用户登录</h1>
            用户名：<input type="text" name="username" id="myname"></br>
            密 &nbsp;&nbsp;码：<input type="password" name="passwd" id="mypassword"></br>
            验证码：<input type="text" name="mycode"><img src="codes.php" style="margin-bottom:-10px"></br>
            <input type="submit" value="登录">
        </form>
        <?php
        }else{
            header("content-type:html;charset=utf-8");
            $hostname="127.0.0.1";
            $username="root";
            $password="123456";
            $database="add";
            $link=mysqli_connect($hostname, $username,
                $password, $database);
            mysqli_set_charset($link, "utf-8");
            $username = $_POST["username"];
            $password = $_POST["passwd"];
            $sql = "select *from user where username='$username'";
            $res = mysqli_query($link,$sql);
            $arr = mysqli_fetch_assoc($res);
            if($arr){
               if($password == $arr["passwd"]){
                   echo "<script>alert('登录成功');window.location.href='index.php'</script>";
               }else{
                   echo "<script>alert('密码错误');window.location.href='login.php'</script>";
               }
            }else{
                echo "<script>alert('账号错误');window.location.href='login.php'</script>";
            }
        }
        
        ?>
    </body>
</html>
